<?php
// 应用公共文件
use think\facade\Env;
use app\common\Str;

if (!function_exists('pr')) {
    /**
     * 数据调试打印
     * @param $var
     */
    function pr($var)
    {
        if (Env::get('app_debug')) {
            if (function_exists('debug_backtrace')) {
                $debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
                if (isset($debug[0]['function']) && $debug[0]['function'] == 'pr') {
                    $template = '<pre style="color:blue;">%s</pre>';
                    printf($template, print_r('in ' . $debug[0]['file'] . " line " . $debug[0]['line'] . ' :', true));
                }
            }
            $template = '<pre style="color:red;">%s</pre>';
            printf($template, print_r($var, true));
        }
    }
}

if (!function_exists('is_json')) {
    /**
     * 判断一个字符串是否是json格式
     * @param $string
     * @return bool
     */
    function is_json($data, $assoc = true)
    {
        if (gettype($data) != 'string') {
            return false;
        }
        if (empty($data)) {
            return false;
        }
        //return !!preg_match('/^(\[|\{).*(\}|\])$/', $data);
        $data = json_decode($data, $assoc);
        if (is_object($data) || is_array($data)) {
            return $data;
        }
        return false;
    }
}


/**
 * 生成css link标签
 * @param $path css文件路径
 * @param bool $abs 是否是绝对路径
 * @return string
 */
function create_css_link($path, $abs = false) {
    $path = is_array($path) ? $path : func_get_args();

    foreach ($path as &$file) {
        // 后缀
        if (!\app\common\Str::endsWith($file, '.css')) {
            $file .= '.css';
        }
        // 自动加css目录
        if ($file[0] != '/') {
            $file = '/css/' . $file;
        }
        $file = '<link rel="stylesheet" href="' . app()->request->realRoot($abs) . 'static' . $file .'"/>';
    }
    return implode("\r\n", $path);
}

function create_js_link($path, $abs = false) {
    $path = is_array($path) ? $path : func_get_args();

    foreach ($path as &$file) {
        // 后缀
        if (!\app\common\Str::endsWith($file, '.js')) {
            $file .= '.js';
        }
        // 自动加js目录
        if ($file[0] != '/') {
            $file = '/js/' . $file;
        }
        $file = '<script src="' . app()->request->realRoot($abs) . 'static' . $file . '" type="text/javascript"></script>';
    }
    return implode("\r\n", $path);
}

function get_extension($path) {
    if (empty($path)) {
        return '';
    }
    return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}

/**
 * 获取一个模型的命名空间
 * @param string $name
 * @param string $app
 * @return string
 */
function get_model_name(string $name, $app = '') {
    $namespace = $name;
    if (strpos($name, '\\') === false) {
        $name = Str::studly($name);
        $app = $app ?: app('http')->getName();
        $namespace = "app\\{$app}\\model\\{$name}";
        if (class_exists($namespace)) {
            return $namespace;
        }
        $namespace = "app\\common\\model\\{$name}";
    }
    if (class_exists($namespace)) {
        return $namespace;
    }
    return '';
}

/**
 * 获取一个模型的命名空间
 * @param string $name
 * @param string $app
 * @return string
 */
function get_validate_name(string $name, $app = '') {
    $namespace = $name;
    if (strpos($name, '\\') === false) {
        $name = Str::studly($name);
        $app = $app ?: app('http')->getName();
        $namespace = "app\\{$app}\\validate\\{$name}";
        if (class_exists($namespace)) {
            return $namespace;
        }
        $namespace = "app\\common\\validate\\{$name}";
    }
    if (class_exists($namespace)) {
        return $namespace;
    }
    return '';
}

/**
 * 获取指定模型名的实例对象
 * @param $name
 * @param bool $newInstance 是否重新new
 * @param string $app
 * @return object|\think\App
 * @throws Exception
 */
function model($name, bool $newInstance = false, $app = '') {
    if (!is_string($name)) {
        return $name;
    }
    $namespace = get_model_name($name, $app);
    if (!$namespace) {
        throw new \Exception('模型不存在' . $name);
    }
    return app($namespace, [], $newInstance);
}
